<?php
include 'base.php';
include 'requireLogin.php';
include 'connectdb.php';
$code = $_GET["code"];

if($_POST){   
    $sql="UPDATE import SET product_code='".$_POST['product_code']."', import_quantity ='".$_POST['quantity']."', date = '".$_POST['date']."', vendor_code='".$_POST['vendor']."' where code ='".$code."'";     
    if (mysql_query($sql, $co))
            echo "Đã cập nhật thành công hóa đơn!";    
    else{                  
            header("HTTP/1.0 404 Not Found");
            header('HTTP', true, 500);
        }                    
}
?>

<script language="JavaScript" type="text/javascript">
    $(document).ready(function () {        
        $( "#date" ).datepicker();                

        $("#submit").click(function(){           
            if($("#product_code").val() == ''|| $("#import_quantity").val() == '' ||  $("#date").val() == '' || $("#vendor").val() == ''){
                alert("Chưa điền đủ thông tin");
                return false;
            }

            var product_code = $('#product_code').val(),
                quantity = $('#import_quantity').val(),                
                date = $('#date').val(),                            
                vendor = $('#vendor').val();
                            
            $.ajax({
                type: "post",
                url: "",
                data: "product_code=" + product_code + "&quantity=" + quantity + "&date=" + date +  "&vendor=" + vendor,
                success: function () {                    
                    $("#notify").text("Đã cập nhật thành công");
                    
                },
                error: function() {                    
                    $("#notify").text("Có lỗi xảy ra");
                }                
            }); 
            return false;

        });
        
    });
</script>
<h1>Sửa record nhập hàng</h1>
<div id ="notify"></div>
<?php    
	$sSQL="SELECT * FROM import where code ='".$code."'";
    $subresult=mysql_query($sSQL);
    if(mysql_num_rows($subresult) > 0){
        while($row = mysql_fetch_array($subresult))                                
            {                                	
        		echo '<div class="form_settings">';
        		echo '<p> <span>Mã</span>'.$row['code'].'</p>';                                
        		echo '<p> <span>Mã sản phẩm</span>'; ?>
                <select id = "product_code" name="product_code">                    
                    <?php
                    $stSQL="select code from product";
                    $resultsub=mysql_query($stSQL);           
                    if(mysql_num_rows($resultsub)> 0)            
                        while($row2 = mysql_fetch_array($resultsub))
                        if ($row2['code'] == $row['product_code']){
                        ?>          
                            <option selected="selected" value="<?php echo"".$row2['code']."";?>"><?php echo"".$row2['code']."";?></option>                    
                        <?php
                        }
                        else{
                            ?>
                            <option value="<?php echo"".$row2['code']."";?>"><?php echo"".$row2['code']."";?></option>                    
                            <?php
                        }         
                        ?>
                    </select></p>
                    <p><span>Số lượng</span><input id = "import_quantity" value="<?php echo $row['import_quantity']; ?>"></p>
                    <p><span>Nhà cung cấp </span>
                    <select name="vendor" id = "vendor">                    
                        <?php
                        $stSQL="select code from vendor";
                        $resultsub=mysql_query($stSQL);           
                        if(mysql_num_rows($resultsub)> 0)            
                            while($row2 = mysql_fetch_array($resultsub))
                            if ($row2['code'] == $row['vendor']){
                            ?>          
                                <option selected="selected" value="<?php echo"".$row2['code']."";?>"><?php echo"".$row2['code']."";?></option>                    
                            <?php
                            }
                            else{
                                ?>
                                <option value="<?php echo"".$row2['code']."";?>"><?php echo"".$row2['code']."";?></option>                    
                                <?php
                            }
                            ?>
                    </select></p>
                <?php                
                echo '<p><span>Ngày nhập</span><input id = "date" type="text" value="'.$row['date'].'"></p>';
        		echo '<span>&nbsp</span><input class="submit" type="submit" id = "submit" name="update" value="Cập nhật" />';            
		        echo '</div>';                             
            }            
    }
    else{
    	echo "Không có mã nhập này";
    }
?>
<?php
include 'footer.php';
?>